package imp

import (
	"fmt"
	"gf-start-kit/library/mlog"
	"gf-start-kit/library/mq/interfaces"
	"github.com/nsqio/go-nsq"
)

//nsq队列实现
type NsqProducerCreate struct {
}
type Producer struct {
	Topic string
	MQ    *nsq.Producer
}

var config = nsq.NewConfig()
var address = "127.0.0.1:4150"

func (p *Producer) SetTopic(topic string) interfaces.IMQProducer {
	p.Topic = topic
	return p
}
func (p *Producer) Pub(msg string) {
	mlog.Println(fmt.Sprintf("topic:%s", p.Topic))
	mlog.Println(fmt.Sprintf("msg:%s", msg))
	if err := p.MQ.Publish(p.Topic, []byte(msg)); err != nil {
		mlog.Fatal(err)
		p.MQ.Stop()
	}
}
func (c *NsqProducerCreate) Create() interfaces.IMQProducer {
	p, err := nsq.NewProducer(address, config)
	if err != nil {
		mlog.Fatal(err)
	}
	return &Producer{MQ: p}
}
